﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InstituteManager.Data;
using InstituteManager.Bussiness;
using System.Data;
using System.Security.Cryptography;

namespace InstituteManager.adm
{
    public partial class CompetitionXL : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pnlMessage.Visible = false;
            if (!IsPostBack)
            {
                gridBind();
            }
        }
        protected void gridBind()
        {
            grvView.DataSource = CompetitionBL.getByAll();
            grvView.DataBind();
        }

        protected void lbtRefresh_Click(object sender, EventArgs e)
        {
            gridBind();
        }

        protected void GeneralDateControls(DropDownList ddlDay, DropDownList ddlMon, DropDownList ddlYear)
        {
            for (int i = 1; i <= 31; i++)
                ddlDay.Items.Add(new ListItem(i + "", i + ""));
            for (int i = 1; i <= 12; i++)
                ddlMon.Items.Add(new ListItem(i + "", i + ""));
            for (int i = 1900; i <= DateTime.Now.Year; i++)
                ddlYear.Items.Add(new ListItem(i + "", i + ""));
        }


        protected void ShowMessage(int MegType, string Message)
        {
            pnlMessage.Visible = true;
            ((Label)pnlMessage.FindControl("lblMessage")).Text = Message;
            switch (MegType)
            {
                case 1:
                    pnlMessage.CssClass = "sukses";
                    break;
                case 0:
                    pnlMessage.CssClass = "gagal";
                    break;
                default:
                    pnlMessage.CssClass = "informasi";
                    break;
            }
        }

        protected void lbtBack_Click(object sender, EventArgs e)
        {
            pnlView.Visible = true;
        }

        protected void grvView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvView.PageIndex = e.NewPageIndex;
            gridBind();
        }

        protected void grvView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strId = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "cmdView":
                    tbl_Competition obj = CompetitionBL.getById(int.Parse(strId));
                    obj = CompetitionBL.getById(int.Parse(strId));
                    lblCompetitionName.Text = obj.CompetitionName;
                    lblStartDate.Text = obj.StartDate.Value.ToString("d");
                    lblEndDate.Text = obj.EndDate.Value.ToString("d");
                    lblCondition.Text = obj.Condition;
                    lblStatus.Text = getCompetitionStatusName(obj);
                    if (obj.tbl_Award != null)
                    {
                        lblAwardName.Text = obj.tbl_Award.AwardName;
                        lblWorth.Text = obj.tbl_Award.Worth.ToString();
                        lblDescription.Text = obj.tbl_Award.Description;
                    }
                    else
                    {
                        lblAwardName.Text = "";
                        lblWorth.Text = "";
                        lblDescription.Text = "";
                    }
                    if (obj.tbl_Staff != null)
                        lblStaffName.Text = obj.tbl_Staff.StaffName;
                    else
                        lblStaffName.Text = "";

                    pnlView.Visible = false;
                    pnlDetail.Visible = true;
                    break;
            }
        }

        protected string getCompetitionStatusName(tbl_Competition CompeObj)
        {
            
                if (CompeObj.StartDate > DateTime.Now)
                    return "Upcoming";
                if (CompeObj.StartDate < DateTime.Now && CompeObj.EndDate > DateTime.Now )
                    return "Ongoing";
                if (CompeObj.EndDate < DateTime.Now)
                    return "Finished";
                return "";
        }

        protected void grvView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
                ((CheckBox)e.Row.FindControl("chkSelectAll")).Attributes.Add("onClick", "SelectAllClick(this)");
            if (e.Row.RowType == DataControlRowType.DataRow)
                ((CheckBox)e.Row.FindControl("chkSelect")).Attributes.Add("onClick", "SelectClick(this)");
        }

        protected void lbtBackDetail_Click(object sender, EventArgs e)
        {
            pnlDetail.Visible = false;
            pnlView.Visible = true;
        }

        protected void ibtSearch_Click(object sender, ImageClickEventArgs e)
        {
            grvView.DataSource = CompetitionBL.SearchByName(txtSearch.Text);
            DataBind();
        }

        protected void ddlFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            grvView.DataSource = CompetitionBL.getCompetitionByStatus(int.Parse(ddlFilter.SelectedValue));
            DataBind();
        }
    }
}